package com.hdyg.sixg.activity.home;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.HostTransAdapter;
import com.hdyg.sixg.entry.ExchangeBean;
import com.hdyg.sixg.mvp.Contrant.CSmartTrans;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PSmartTrans;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.List;
import butterknife.BindView;

/**
 * @author CZB
 * @describe 托管钱包转出
 * @time 2019/4/9 11:35
 */

public class HostTransActivity extends BaseActivity implements CSmartTrans.IVSmartTrans {

    private final static String DEPOSIT_WALLET_TYPE = "1",//定存钱包
            CURRENT_WALLET_TYPE = "2";//活期钱包

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_deposit)
    TextView tvDeposit;
    @BindView(R.id.tv_current)
    TextView tvCurrent;
    @BindView(R.id.sv_trans)
    SuperTextView svTrans;
    @BindView(R.id.sv_fee_percent)
    SuperTextView svFeePercent;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CSmartTrans.IPSmartTrans mPresenter;
    private List<ExchangeBean.ListBean> depositDatas, currentDatas, datasFlag;
    private String depositFee, currentFee, feeFlag;//定存费率 活期费率
    private String type = DEPOSIT_WALLET_TYPE;
    private String id, trapass;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_smart_trans;
    }

    @Override
    protected void initView() {
        onClickListener();
        setChangeUI(true);
        tvTopTitle.setText(R.string.invers_trans);

    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
        });
        llTopLeft.setOnClickListener(v -> finish());
        //定存
        tvDeposit.setOnClickListener(v -> {
            setChangeUI(true);
        });
        //活期
        tvCurrent.setOnClickListener(v -> {
            setChangeUI(false);
        });
        //显示定存/活期列表窗口
        svTrans.setOnSuperTextViewClickListener(superTextView -> {
            showTransTypePop(superTextView);
        });
        //确定提现
        btSure.setOnClickListener(v -> {

            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(id)) {
                toastNotifyShort(R.string.toast_trans_type);
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.regist_trans_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()) {
                return;
            }
            if (StringUtil.isFastDoubleClick()) {
                return;
            }
            mPresenter.pGetSmartTrans(BaseUrl.HOST_TRANS_URL, GetParamUtil.getApplySmartTransParam(type, id, trapass));
        });
    }

    /**
     * 改变UI颜色
     *
     * @param flag true选择定存  false选择活期
     */
    private void setChangeUI(boolean flag) {
        if (flag) {
            tvDeposit.setTextColor(getResources().getColor(R.color.main_color));
            tvCurrent.setTextColor(getResources().getColor(R.color.gray_80));
            type = DEPOSIT_WALLET_TYPE;
            if (depositDatas == null) {
                initData();
            }
        } else {
            tvDeposit.setTextColor(getResources().getColor(R.color.gray_80));
            tvCurrent.setTextColor(getResources().getColor(R.color.main_color));
            type = CURRENT_WALLET_TYPE;
            if (currentDatas == null) {
                initData();
            }
        }
        //每次点击切换定存与活期  都重置转出和手续费
        svFee.setRightString("");
        svTrans.setRightString("");

        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    //显示转出类型窗口
    private void showTransTypePop(View v) {
        HostTransAdapter transAdapter;
        View view = View.inflate(mContext, R.layout.pop_host_trans, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_content);
        TextView tvCancle = view.findViewById(R.id.tv_cancle);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvCancle.setOnClickListener(v1 -> mPopupWindow.dismiss());
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        if (type.equals(DEPOSIT_WALLET_TYPE)) {
            datasFlag = depositDatas;
            feeFlag = depositFee;
            tvTitle.setText(R.string.pop_trans_title_2);
        } else {
            datasFlag = currentDatas;
            feeFlag = currentFee;
            tvTitle.setText(R.string.pop_trans_title);
        }
        transAdapter = new HostTransAdapter(R.layout.item_pop_host_trans, datasFlag);
        rvArea.setAdapter(transAdapter);
        transAdapter.setOnItemClickListener((adapter, view1, position) -> {
            id = datasFlag.get(position).getId();
            svTrans.setRightString(datasFlag.get(position).getDesc());
            //手续费 = 数量 * 手续费 / 100
            double feeStr = Double.valueOf(datasFlag.get(position).getAmount()) * Double.valueOf(feeFlag) / 100;
            svFee.setRightString(String.valueOf(feeStr));
            for (int i = 0; i < datasFlag.size(); i++) {
                if (i == position) {
                    datasFlag.get(i).setIs_select(true);
                } else {
                    datasFlag.get(i).setIs_select(false);
                }
            }
            transAdapter.replaceData(datasFlag);
            //更改选中状态  重新赋值
            if (type.equals(DEPOSIT_WALLET_TYPE)) {
                depositDatas = datasFlag;
            } else {
                currentDatas = datasFlag;
            }
            transAdapter.notifyDataSetChanged();
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showLocation(mContext, view, Gravity.BOTTOM, 0, 0, 0);

    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.HOST_CONVER_URL, GetParamUtil.getSmartTransParam(type));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PSmartTrans(this);
    }

    @Override
    public void vGetInitSuccess(ExchangeBean dataBean) {

        if (type.equals(DEPOSIT_WALLET_TYPE)) {
            depositDatas = dataBean.getList();
            depositFee = dataBean.getRate();
        } else {
            currentDatas = dataBean.getList();
            currentFee = dataBean.getRate();
        }
        String str = StringUtil.getPercentage(Float.valueOf(dataBean.getRate()), 100);
        svFeePercent.setRightString(str);
    }

    @Override
    public void vGetSmartTransSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }

}
